#include"../login/campus_login_redis_client.h"
#include "campus_login_redis_client.h"
#include"../log/campus_logger.h"

CampusLoginRedisClient::CampusLoginRedisClient(std::string const &redis_url, int redis_port, int timeout_ms):
    CampusRedisConnector(redis_url,redis_port,timeout_ms)
{

}

CampusLoginRedisClient::~CampusLoginRedisClient()
{
}

enum CampusLoginRedisClientRet CampusLoginRedisClient::setUserOnline(std::string const &uid,int expired_ms)
{
    int ret = this->set(constructKey(CAMPUS_REDIS_KEY_ONLINE, uid), "1", expired_ms);
    if(ret == 0){
        CampusLogger::info("setUserOnline: {} success", uid);
        return CAMPUS_LOGIN_REDIS_CLIENT_RET_SUCCESS;
    }
    else{
        CampusLogger::info("setUserOnline: {} fail", uid);
        return CAMPUS_LOGIN_REDIS_CLIENT_RET_FAIL;
    }

    return CAMPUS_LOGIN_REDIS_CLIENT_RET_SUCCESS;
}

enum CampusLoginRedisClientRet CampusLoginRedisClient::delUserOnline(std::string const &uid)
{
    int ret = this->del(constructKey(CAMPUS_REDIS_KEY_ONLINE, uid));
    if(ret == 0){
        CampusLogger::info("delUserOnline: {} success", uid);
        return CAMPUS_LOGIN_REDIS_CLIENT_RET_SUCCESS;
    }
    else{
        CampusLogger::info("delUserOnline: {} fail", uid);
        return CAMPUS_LOGIN_REDIS_CLIENT_RET_FAIL;
    }

    return CAMPUS_LOGIN_REDIS_CLIENT_RET_SUCCESS;
}

enum CampusLoginRedisClientRet CampusLoginRedisClient::existUserOnline(std::string const & uid)
{
    int ret = this->exists(constructKey(CAMPUS_REDIS_KEY_ONLINE, uid));
    if(ret != 0){
        return CAMPUS_LOGIN_REDIS_CLIENT_RET_NOT_EXIST;
    }
    else{
        return CAMPUS_LOGIN_REDIS_CLIENT_RET_EXIST;
    }
}

std::string CampusLoginRedisClient::constructKey(std::string const &prefix, std::string const &suffix)
{
    CampusLogger::info("constructKey: {}:{}", prefix, suffix);
    return std::string(prefix + ":" + suffix);
}
